<?php
/**
 * Created by PhpStorm.
 * User: zhongbaitong
 * Date: 2019/9/29
 * Time: 15:54
 */

namespace app\forum\model;


class PostLike extends ForumBase
{
    // 是否已喜欢
    // $type: 1-帖子 2-留言 3-回复
    public static function isliked($id, $user_id, $type = 1)
    {
        $type_info = ["null", "post_id"
            ,"comment_id", "reply_id"];

        $where = compact("user_id");
        $where[$type_info[$type]] = $id;

        return self::where($where)->count();
    }

    // 点赞处理
    public static function handle($params)
    {
        $query = self::where($params);

        if ($query->count()) {
            $query->delete();
            $code = 2;
            $msg = "取消成功";
        } else {
            self::create($params);
            $code = 1;
            $msg = "点赞成功";
        }

        return compact('code', 'msg');
    }
}